<template>
  <el-container class="home-container">
    <!-- 主页头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="primary" @click="logOut">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" >
        <div class="toggle-button" @click="toggleMenu">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          :unique-opened="true"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              v-for="value in item.children"
              :key="value.id"
              :index="'/' + value.path"
              @click="saveNavState('/' + value.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ value.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主页主体 -->
      <el-main :class="[isCollapse ? 'main' : 'elmain']">
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('active')
  },
  data() {
    return {
      //左侧菜单数据
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    //退出功能
    logOut() {
      this.$MessageBox
        .confirm('你确定要退出吗？', '退出提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          //清除sessionStorage
          window.sessionStorage.clear()
          //跳转到登陆页面
          this.$router.push('/login')
          this.$message({
            type: 'success',
            message: '退出成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消退出操作'
          })
        })
    },
    //获取所有菜单列表
    async getMenuList() {
      const res = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    //点击，折叠或者打开菜单栏
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    },
    //保存连接的激活状态
    saveNavState(path) {
      window.sessionStorage.setItem('active', path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  position: fixed;
  width: 100%;
  z-index: 999;
  > div {
    display: flex;
    align-items: center;
    span {
      margin: 15px;
    }
  }
}
.el-aside {
  background-color: #313743;
  height: 100%;
  position: fixed;
  // box-sizing: border-box;
  margin-top: 60px;
  z-index: 999;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #e9edf1;
  margin-left: 200px;
  margin-top: 56px;
}
.elmain{
  margin-left: 200px;
}
.main{
  margin-left: 66px;
}

.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #475163;
  font-size: 10px;
  color: #fff;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
